package com.neusoft.weekreport.component.controller;


import com.neusoft.weekreport.component.bizService.RoleBizService;
import com.neusoft.weekreport.component.req.GetAddRoleInfoReq;
import com.neusoft.weekreport.component.req.GetRoleInfoByIdReq;
import com.neusoft.weekreport.component.resp.GetAddRoleInfoResp;
import com.neusoft.weekreport.component.resp.GetRoleInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetRoleInfoListResp;
import com.neusoft.weekreport.component.resp.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping("/role")
public class RoleController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    RoleBizService roleBizService;

    //查询角色树
    @RequestMapping(value = "/roleManagement", method = RequestMethod.GET)
    public ResponseBean<GetRoleInfoListResp>  getRoleInfoList(HttpServletRequest request) {
        ResponseBean<GetRoleInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetRoleInfoListResp resp = roleBizService.getRoleInfoList();
            responseBean = new ResponseBean(HttpStatus.OK.value(),"获取角色树成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //通过id得到角色信息
    @RequestMapping(value = "/roleById",method = RequestMethod.GET)
    public ResponseBean<GetRoleInfoByIdResp>  getRoleInfoById(GetRoleInfoByIdReq req,HttpServletRequest request) {
        ResponseBean<GetRoleInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetRoleInfoByIdResp resp = roleBizService.getRoleInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"获取角色信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //添加角色
    @RequestMapping(value = "/roleManagement", method = RequestMethod.POST)
    public ResponseBean<GetAddRoleInfoResp> getAddRoleInfo(@RequestBody GetAddRoleInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddRoleInfoResp> responseBean = new ResponseBean<>();
        try{
            GetAddRoleInfoResp resp  = roleBizService.getAddRoleInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"添加角色信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //修改角色
    @RequestMapping(value = "/roleManagement", method = RequestMethod.PUT)
    public ResponseBean<GetRoleInfoByIdResp> getEditRoleInfo(@RequestBody GetAddRoleInfoReq req, HttpServletRequest request){
        ResponseBean<GetRoleInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetRoleInfoByIdResp resp = new GetRoleInfoByIdResp();
            roleBizService.getEditRoleInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"修改角色信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //删除角色
    @RequestMapping(value = "/roleManagement", method = RequestMethod.DELETE)
    public ResponseBean<GetRoleInfoByIdResp> getDeleteRoleInfo(@RequestBody GetAddRoleInfoReq req, HttpServletRequest request){
        ResponseBean<GetRoleInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetRoleInfoByIdResp resp = new GetRoleInfoByIdResp();
            roleBizService.getDeleteRoleInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"删除角色成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }
}
